package com.winit.openapi.isp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.isp.YanWenOrderService;
import com.winit.oms.spi.isp.command.vendor.yanwen.YanWenOrderLibraryCommand;
import com.winit.oms.spi.isp.command.vendor.yanwen.YanWenOrderStorageCommand;
import com.winit.oms.spi.isp.vo.YanWenOrder;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;

@Controller
public class ISPOrderWarehouseController extends BaseController {

    private Logger               logger = LoggerFactory.getLogger(ISPOrderWarehouseController.class);

    @Resource
    private YanWenOrderService   yanWenOrderService;
    
    
    /**
     * 燕文修改跟踪号接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/isp/order/replaceExpressNo")
    public String replaceExpressNo(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 校验参数
            validReplaceExpressNo(json);
            // 解析报文
            List<YanWenOrder> yanWenOrders = buildYanWenOrder(json);
            YanWenOrderLibraryCommand command = new YanWenOrderLibraryCommand();
            command.setYanWenOrder(yanWenOrders);
            yanWenOrderService.updateExpressNo(command);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
	/**
     * 燕文修改重量接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/isp/order/confirmWeight")
    public String confirmWeight(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 校验参数
            validConfirmWeight(json);
            // 解析报文
            List<YanWenOrder> yanWenOrders = buildYanWenOrder(json);
            YanWenOrderLibraryCommand command = new YanWenOrderLibraryCommand();
            command.setYanWenOrder(yanWenOrders);
            yanWenOrderService.updateWeight(command);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
    

    /**
     * 燕文ascan接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/isp/order/addAscan")
    public String addAscan(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 校验参数
            validISPOrderAscan(json);
            // 解析报文
            List<YanWenOrder> yanWenOrders = buildYanWenOrder(json);
            YanWenOrderLibraryCommand command = new YanWenOrderLibraryCommand();
            command.setYanWenOrder(yanWenOrders);
            yanWenOrderService.addOrderAscanTracking(command);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }

    /**
     * 燕文入库接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/isp/order/storage")
    public String warehousing(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 校验参数
            validISPOrder(json);
            // 解析报文
            List<YanWenOrder> yanWenOrders = buildYanWenOrder(json);
            YanWenOrderStorageCommand storageCommand = new YanWenOrderStorageCommand();
            storageCommand.setYanWenOrder(yanWenOrders);
            yanWenOrderService.updateOrderStorage(storageCommand);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }

    /**
     * 打包接口 TODO Add comments here.
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/isp/order/pack")
    public String pack(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 校验参数
            validISPOrderPack(json);
            // 解析报文
            List<YanWenOrder> yanWenOrders = buildYanWenOrder(json);
            YanWenOrderLibraryCommand libraryCommand = new YanWenOrderLibraryCommand();
            libraryCommand.setYanWenOrder(yanWenOrders);
            yanWenOrderService.addPackTracking(libraryCommand);

        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;

    }

    /**
     * 燕文出库后续轨迹接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/isp/order/library")
    public String library(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 校验参数
            validISPOrderTracking(json);

            // 解析报文
            List<YanWenOrder> yanWenOrders = buildYanWenOrder(json);
            YanWenOrderLibraryCommand command = new YanWenOrderLibraryCommand();
            command.setYanWenOrder(yanWenOrders);
            command.setCtx(CommandContext.getContext());
            yanWenOrderService.updateOrderLibrary(command);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }

    
    /**
     * 燕文口岸仓出库接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/isp/order/portLibrary")
    public String portLibrary(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 校验参数
            validISPOrderPortLibrary(json);
            // 解析报文
            List<YanWenOrder> yanWenOrders = buildYanWenOrder(json);
            YanWenOrderLibraryCommand command = new YanWenOrderLibraryCommand();
            command.setYanWenOrder(yanWenOrders);
            command.setCtx(CommandContext.getContext());
            yanWenOrderService.addPortLibrary(command);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
    /**
     * 验证参数
     * 
     * @param json
     */
    private void validISPOrderAscan(JSONObject json) {
        if (json.containsKey("orderList")) {
            JSONArray packageList = json.getJSONArray("orderList");
            for (Object obj : packageList) {
                JSONObject pack = (JSONObject) obj;
                ValidateUtil.validNotNull(pack, "expressNo");
                ValidateUtil.validNotNull(pack, "orderCode");
                ValidateUtil.validNotNull(pack, "occurTime");
                ValidateUtil.valid(pack, "portLocation", 20, true);
                ValidateUtil.validMaxLength(pack, "expressNo", 60);
                ValidateUtil.validMaxLength(pack, "remark", 256);
                ValidateUtil.validMaxLength(pack, "orderCode", 32);
                ValidateUtil.validDateString(pack, "occurTime", ValidateUtil.DATE_PATTERN_BASE);
            }
        } else {
            logger.info("订单信息为空");
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "orderList");
        }
    }
    /**
     * 验证参数
     * 
     * @param json
     */
    private void validISPOrder(JSONObject json) {
        if (json.containsKey("orderList")) {
            JSONArray packageList = json.getJSONArray("orderList");
            for (Object obj : packageList) {
                JSONObject pack = (JSONObject) obj;
                ValidateUtil.validNotNull(pack, "expressNo");
                ValidateUtil.validNotNull(pack, "occurTime");
                ValidateUtil.validMaxLength(pack, "expressNo", 60);
                ValidateUtil.validMaxLength(pack, "remark", 256);
                ValidateUtil.validMaxLength(pack, "orderCode", 32);
                ValidateUtil.validDateString(pack, "occurTime", ValidateUtil.DATE_PATTERN_BASE);
            }
        } else {
            logger.info("订单信息为空");
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "orderList");
        }
    }
    /**
     * 验证参数
     * 
     * @param json
     */
    private void validISPOrderPack(JSONObject json) {
        if (json.containsKey("orderList")) {
            JSONArray packageList = json.getJSONArray("orderList");
            for (Object obj : packageList) {
                JSONObject pack = (JSONObject) obj;
                ValidateUtil.validNotNull(pack, "expressNo");
                ValidateUtil.valid(pack, "weight", 16, true);
                ValidateUtil.validBigDecimal(pack, "weight", true);
                ValidateUtil.validBigDecimal(pack, "height", false);
                ValidateUtil.validMaxLength(pack, "height", 16,false);
                ValidateUtil.validBigDecimal(pack, "width", false);
                ValidateUtil.validMaxLength(pack, "width", 16,false);
                ValidateUtil.validBigDecimal(pack, "length", false);
                ValidateUtil.validMaxLength(pack, "length", 16,false);
                ValidateUtil.validNotNull(pack, "occurTime");
                ValidateUtil.validMaxLength(pack, "expressNo", 60);
                ValidateUtil.validMaxLength(pack, "remark", 256);
                ValidateUtil.validMaxLength(pack, "orderCode", 32);
                ValidateUtil.validDateString(pack, "occurTime", ValidateUtil.DATE_PATTERN_BASE);
            }
        } else {
            logger.info("订单信息为空");
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "orderList");
        }
    }

    /**
     * 构建燕文订单
     * 
     * @param json
     * @return
     */
    private List<YanWenOrder> buildYanWenOrder(JSONObject json) {
        List<YanWenOrder> listOrders = new ArrayList<YanWenOrder>();
        YanWenOrder yanWenOrder = null;
        if (json.containsKey("orderList")) {
            JSONArray packageList = json.getJSONArray("orderList");
            for (Object obj : packageList) {
                JSONObject pack = (JSONObject) obj;
                yanWenOrder = new YanWenOrder();
                if (pack.containsKey("orderCode")) {
                    yanWenOrder.setOrderCode(pack.getString("orderCode"));
                }
                if (pack.containsKey("expressNo")) {
                    yanWenOrder.setExpressNo(pack.getString("expressNo"));
                }
                if (pack.containsKey("occurTime")) {
                    yanWenOrder.setOccurTime(pack.getDate("occurTime"));
                }
                if (pack.containsKey("weight")) {
                    yanWenOrder.setWeigth(pack.getBigDecimal("weight"));
                }
                if (pack.containsKey("height")) {
                    yanWenOrder.setHeight(pack.getBigDecimal("height"));
                }
                if (pack.containsKey("width")) {
                    yanWenOrder.setWidth(pack.getBigDecimal("width"));
                }
                if (pack.containsKey("length")) {
                    yanWenOrder.setLength(pack.getBigDecimal("length"));
                }
                if (pack.containsKey("remark")) {
                    yanWenOrder.setRemark(pack.getString("remark"));
                }
                if (pack.containsKey("bigPackageNo")) {
                    yanWenOrder.setBigPackageNo(pack.getString("bigPackageNo"));
                }
                if (pack.containsKey("bigPackageTotalWeight")) {
                    yanWenOrder.setBigPackageTotalWeight(pack.getBigDecimal("bigPackageTotalWeight"));
                }
                if (pack.containsKey("bigPackageHeight")) {
                    yanWenOrder.setBigPackageHeight(pack.getBigDecimal("bigPackageHeight"));
                }
                if (pack.containsKey("bigPackageWidth")) {
                    yanWenOrder.setBigPackageWidth(pack.getBigDecimal("bigPackageWidth"));
                }
                if (pack.containsKey("bigPackageLength")) {
                    yanWenOrder.setBigPackageLength(pack.getBigDecimal("bigPackageLength"));
                }
                if (pack.containsKey("portLocation")) {
                    yanWenOrder.setPortLocation(pack.getString("portLocation"));
                }
                if (pack.containsKey("trackCode")) {
                    yanWenOrder.setTrackCode(pack.getString("trackCode"));
                }
                if (pack.containsKey("oldExpressNo")) {
                    yanWenOrder.setOldExpressNo(pack.getString("oldExpressNo"));
                }
                listOrders.add(yanWenOrder);
            }
        }
        return listOrders;
    }
    /**
     * 验证接收出库后续轨迹接口参数
     * 
     * @param json
     */
    private void validISPOrderTracking(JSONObject json) {
        if (json.containsKey("orderList")) {
            JSONArray orderList = json.getJSONArray("orderList");
            for (Object obj : orderList) {
                JSONObject order = (JSONObject) obj;
                ValidateUtil.valid(order, "expressNo", 60, true);
                ValidateUtil.valid(order, "orderCode", 32, true);
                ValidateUtil.validNotNull(order, "occurTime");
                ValidateUtil.validDateString(order, "occurTime", ValidateUtil.DATE_PATTERN_BASE);
                ValidateUtil.valid(order, "portLocation", 20, true);
                ValidateUtil.valid(order, "trackCode", 10, true);
                ValidateUtil.validMaxLength(order, "remark", 256);
            }
        } else {
            logger.info("接受燕文出库后续轨迹的订单信息为空");
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "orderList");
        }
    }
    
    /**
     * 验证口岸仓出库接口参数
     * 
     * @param json
     */
    private void validISPOrderPortLibrary(JSONObject json) {
        if (json.containsKey("orderList")) {
            JSONArray orderList = json.getJSONArray("orderList");
            for (Object obj : orderList) {
                JSONObject order = (JSONObject) obj;
                ValidateUtil.valid(order, "expressNo", 60, true);
                ValidateUtil.validMaxLength(order, "orderCode", 32);
                ValidateUtil.validNotNull(order, "occurTime");
                ValidateUtil.validDateString(order, "occurTime", ValidateUtil.DATE_PATTERN_BASE);
                ValidateUtil.valid(order, "portLocation", 20, true);
                ValidateUtil.validBigDecimal(order, "weight", false);
                ValidateUtil.validMaxLength(order, "weight", 16,false);
                ValidateUtil.validBigDecimal(order, "height", false);
                ValidateUtil.validMaxLength(order, "height", 16,false);
                ValidateUtil.validBigDecimal(order, "width", false);
                ValidateUtil.validMaxLength(order, "width", 16,false);
                ValidateUtil.validBigDecimal(order, "length", false);
                ValidateUtil.validMaxLength(order, "length", 16,false);
                ValidateUtil.validMaxLength(order, "bigPackageTotalWeight", 16, false);
                ValidateUtil.validBigDecimal(order, "bigPackageTotalWeight", false);
                ValidateUtil.validBigDecimal(order, "bigPackageHeight", false);
                ValidateUtil.validMaxLength(order, "bigPackageHeight", 16,false);
                ValidateUtil.validBigDecimal(order, "bigPackageWidth", false);
                ValidateUtil.validMaxLength(order, "bigPackageWidth", 16,false);
                ValidateUtil.validBigDecimal(order, "bigPackageLength", false);
                ValidateUtil.validMaxLength(order, "bigPackageLength", 16,false);
                ValidateUtil.validMaxLength(order, "bigPackageNo", 60);
                ValidateUtil.validMaxLength(order, "remark", 256);
            }
        } else {
            logger.info("口岸仓出库接口的订单信息为空");
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "orderList");
        }
    }
    private void validConfirmWeight(JSONObject json) {
        if (json.containsKey("orderList")) {
            JSONArray packageList = json.getJSONArray("orderList");
            for (Object obj : packageList) {
                JSONObject pack = (JSONObject) obj;
                ValidateUtil.validNotNull(pack, "expressNo");
                ValidateUtil.validNotNull(pack, "orderCode");
                ValidateUtil.valid(pack, "weight", 16, true);
                ValidateUtil.validBigDecimal(pack, "weight", true);
                ValidateUtil.validMaxLength(pack, "expressNo", 60);
                ValidateUtil.validMaxLength(pack, "orderCode", 32);
                ValidateUtil.validBigDecimal(pack, "height", false);
                ValidateUtil.validMaxLength(pack, "height", 16,false);
                ValidateUtil.validBigDecimal(pack, "width", false);
                ValidateUtil.validMaxLength(pack, "width", 16,false);
                ValidateUtil.validBigDecimal(pack, "length", false);
                ValidateUtil.validMaxLength(pack, "length", 16,false);
            }
        } else {
            logger.info("订单信息为空");
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "orderList");
        }
    }
    private void validReplaceExpressNo(JSONObject json) {
  	  if (json.containsKey("orderList")) {
            JSONArray packageList = json.getJSONArray("orderList");
            for (Object obj : packageList) {
                JSONObject pack = (JSONObject) obj;
                ValidateUtil.validNotNull(pack, "expressNo");
                ValidateUtil.validNotNull(pack, "orderCode");
                ValidateUtil.validNotNull(pack, "oldExpressNo");
                ValidateUtil.validMaxLength(pack, "expressNo", 60);
                ValidateUtil.validMaxLength(pack, "oldExpressNo", 60);
                ValidateUtil.validMaxLength(pack, "orderCode", 60);
                ValidateUtil.validNotNull(pack, "occurTime");
                ValidateUtil.validDateString(pack, "occurTime", ValidateUtil.DATE_PATTERN_BASE);
                ValidateUtil.validMaxLength(pack, "remark", 256);
            }
        } else {
            logger.info("订单信息为空");
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "orderList");
        }
	}
}
